<template>
  <!-- 导航部分开始 -->
<!--    <el-row style="margin-top: 10px;">-->
<!--        <el-col :span="24">-->
<!--            <el-breadcrumb separator="/">-->
<!--                <el-breadcrumb-item>首页</el-breadcrumb-item>-->
<!--                <el-breadcrumb-item>字典管理</el-breadcrumb-item>-->
<!--            </el-breadcrumb>-->
<!--        </el-col>-->
<!--    </el-row>-->
  <!-- 导航部分结束 -->
  <!-- 搜索栏部分开始 -->
    <el-row style="margin-top: 10px;">
        <el-col :span="24">
            <!-- model 绑定表单数据 -->
            <el-form :inline="true" :model="queryDict" class="demo-form-inline">
                <el-form-item label="字典名称">
                    <el-input v-model="queryDict.dictName" placeholder="字典名称"/>
                </el-form-item>
                <el-form-item label="字典类别">
                    <el-input v-model="queryDict.dictType" placeholder="字典类别"/>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select
                            style="width: 100px"
                            v-model="queryDict.dictStatus"
                            placeholder="选择状态"
                    >
                        <el-option value="0" :selected="true" label="全部"/>
                        <el-option value="1" label="正常"/>
                        <el-option value="2" label="废弃"/>
                        <el-option value="99" label="删除"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-date-picker
                            v-model="createTimes"
                            type="daterange"
                            range-separator="To"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                            value-format="YYYY-MM-DD"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="loadDict">搜索</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
  <!-- 搜索栏部分结束 -->

  <!-- 工具栏部分开始 -->
    <el-row style="margin-top: 10px;">
        <el-col :span="24">
            <el-tooltip
                    class="box-item"
                    effect="dark"
                    content="新增字典"
                    placement="top-start"
            >
                <el-button @click="toAddDict" type="primary">
                    <el-icon>
                        <Plus/>
                    </el-icon>
                </el-button>
            </el-tooltip>
            <el-button @click="editBtnHandler" :disabled="editBtnDisabled" type="primary">
                <el-icon>
                    <EditPen/>
                </el-icon>
            </el-button>
            <el-popconfirm confirm-button-text="是" cancel-button-text="否"
                           @confirm="deleteBtnHandler" title="您确定要删除选中的典信息吗？">
                <template #reference>
                    <el-button :disabled="deleteBtnDisabled" type="primary">
                        <el-icon>
                            <Minus/>
                        </el-icon>
                    </el-button>
                </template>
            </el-popconfirm>
<!--            <el-button type="primary">-->
<!--                <el-icon>-->
<!--                    <Upload/>-->
<!--                </el-icon>-->
<!--            </el-button>-->
<!--            <el-button type="primary">-->
<!--                <el-icon>-->
<!--                    <Download/>-->
<!--                </el-icon>-->
<!--            </el-button>-->
        </el-col>
    </el-row>
  <!-- 工具栏部分结束 -->
  <!-- 列表部分开始 -->
    <el-row style="margin-top: 10px;">
        <el-col :span="24">
            <el-table ref="dictTaebl" @selection-change="handleSelectionChange" :data="dicts" border :stripe="true"
                      style="width: 95%">
                <el-table-column type="selection" width="55"/>
                <el-table-column prop="dictId" label="#"/>
                <el-table-column prop="dictName" label="字典名称"/>
                <el-table-column prop="dictType" label="字典类型">
                    <template #default="scope">
                        <el-link @click="showDictItem(scope.row.dictType)" type="primary">{{ scope.row.dictType }}
                        </el-link>
                    </template>
                </el-table-column>
                <el-table-column prop="dictNotes" label="字典备注"/>
                <el-table-column prop="dictStatus" label="状态">
                    <template #default="scope">
                        <el-tag type="success" v-if="scope.row.dictStatus == 1">正常</el-tag>
                        <el-tag type="warning" v-else-if="scope.row.dictStatus == 2">废弃</el-tag>
                        <el-tag type="danger" v-else>删除</el-tag>
                    </template>
                </el-table-column>
                <el-table-column width="180" prop="createTime" label="创建时间"/>
                <!-- 行工具栏 -->
                <el-table-column width="140" label="操作">
                    <template #default="scope">
                        <el-button type="primary" @click="toEditDict(scope.row.dictId)" size="small">编辑</el-button>
                        <el-popconfirm confirm-button-text="是" cancel-button-text="否"
                                       @confirm="deleteDict(scope.row.dictId)" title="您确定要删除这个字典信息吗？">
                            <template #reference>
                                <el-button type="danger" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </el-col>
    </el-row>
  <!-- 列表部分结束 -->
  <!-- 分页部分开始 -->
    <el-row style="margin-top: 10px;">
        <el-col :span="24">
            <el-pagination background @change="pageChange" layout="prev, pager, next" v-bind:page-size="limit"
                           :total="total"/>
        </el-col>
    </el-row>
  <!-- 分页部分结束 -->

  <!-- 使用子组件 -->
    <DictAddWin :visible="addDictVisble" @optionCompleted="addOptionCompleted"/>
    <DictEditWin :visible="editDictVisble" :dictId="editDictId" @optionCompleted="editOptionCompleted"/>
    <DictItem :visible="dictItemVisible" :dictType="currentDictType" @optionCompleted="dictItemOptionCompleted"/>
</template>

<script>
import DictAddWin from './dict-add-win.vue';
import DictEditWin from './dict-edit-win.vue';
import DictItem from './dict-item.vue';
// 引入api
import dictRequest from '../../../api/dict.js';

export default {
    components: {
        DictAddWin,
        DictEditWin,
        DictItem
    },
    name: "dict.vue",
    data() {
        return {
            queryDict: {},// 绑定查询条件
            createTimes: [],// 绑定搜索栏中的时间范围
            dicts: [],// 列表数据
            currentPage: 1,// 当前页码
            limit: 10,// 每页大小
            total: 0,// 总条数
            addDictVisble: false,// 新增字典的弹窗是否显示

            editDictVisble: false,// 标记是否显示编辑字典的弹窗
            editDictId: -1,// 编辑字典的id

            dictItemVisible: false,// 标记是否显示字典项的弹窗
            currentDictType: undefined,// 当前字典的type

            editBtnDisabled: true,
            deleteBtnDisabled: true
        }
    }, methods: {
        /**
         * 工具栏删除按钮事件
         */
        deleteBtnHandler() {
            let selectionRows = this.$refs.dictTaebl.getSelectionRows();
            let dictIds = new Array();
            selectionRows.forEach(row => {
                dictIds.push(row.dictId);
            })
            dictRequest.deleteDicts(dictIds, res => {
                this.$message({
                    type: 'success',
                    message: '删除字典成功!'
                });
                this.loadDict();
            });
        },
        /**
         * 行删除按钮事件
         * @param dictId
         */
        deleteDict(dictId) {
            dictRequest.deleteDict(dictId, res => {
                this.$message({
                    type: 'success',
                    message: '删除字典成功!'
                });
                this.loadDict();
            });
        },
        /**
         * 工具栏编辑按钮事件
         */
        editBtnHandler() {
            let editDict = this.$refs.dictTaebl.getSelectionRows()[0];
            if (editDict != undefined) {
                this.editDictId = editDict.dictId;
                this.editDictVisble = true;
            } else {
                this.$message({
                    type: 'warning',
                    message: '请选择要编辑的字典!'
                });
            }
        },
        /**
         * 表格中数据选择事件
         */
        handleSelectionChange() {
            let selectionRows = this.$refs.dictTaebl.getSelectionRows();
            if (selectionRows.length == 0) {
                this.editBtnDisabled = true;
                this.deleteBtnDisabled = true;
            } else if (selectionRows.length == 1) {
                this.editBtnDisabled = false;
                this.deleteBtnDisabled = false;
            } else {
                this.editBtnDisabled = true;
                this.deleteBtnDisabled = false;
            }
        },
        /**
         * 字典列表中字典类别的超链接事件
         * @param dictType
         */
        showDictItem(dictType) {
            this.currentDictType = dictType;
            // 弹窗
            this.dictItemVisible = true;
        },
        /**
         * 监听子组件字典项弹窗的操作是否完成
         */
        dictItemOptionCompleted() {
            this.currentDictId = -1;
            this.dictItemVisible = false;
        },
        /**
         * 监听编辑字典的子组件操作完成的事件
         */
        editOptionCompleted() {
            // 将弹窗设置为false
            this.editDictVisble = false;
            // 重新加载列表
            this.loadDict();
        },
        /**
         * 行编辑按钮事件
         */
        toEditDict(dictId) {
            // 将传入的dictId设置到editDictId
            this.editDictId = dictId;
            // 弹出编辑窗体
            this.editDictVisble = true;
        },
        /**
         * 工具栏的新增字典按钮事件
         */
        toAddDict() {
            // 弹窗 addDictVisble设置为true
            this.addDictVisble = true;
        },
        /**
         * 监听添加字典的子组件操作完成的事件
         */
        addOptionCompleted() {
            // 将弹窗设置为false
            this.addDictVisble = false;
            // 重新加载列表
            this.loadDict();
        },
        /**
         * 翻页的事件
         * @Param currentPage 最新的页码
         */
        pageChange(currentPage, pageSize) {
            // 将最新的页码赋值给this.currentPage
            this.currentPage = currentPage;
            // 重新加载数据
            this.loadDict();
        },
        /**
         * 加载字典信息
         */
        loadDict() {
            // 设置其他的条件
            this.queryDict.page = this.currentPage;
            this.queryDict.limit = this.limit;
            console.log(this.createTimes);
            // 设置时间
            if (this.createTimes != undefined && this.createTimes.length > 0)
                this.queryDict.createTimeStart = this.createTimes[0];
            if (this.createTimes != undefined && this.createTimes.length > 1)
                this.queryDict.createTimeEnd = this.createTimes[1];
            dictRequest.loadDict(this.queryDict, res => {
                this.dicts = res.data.data;
                this.total = res.data.total;
            })
        }
    }, mounted() {
        this.loadDict();
    }
}
</script>

<style scoped>

</style>